﻿using System.Collections;
using System.Collections.Generic;

namespace SCore.Function.Fixed
{
    public class fpbounds
    {
        public fpbounds GetInstance(List<fp3> points)
        {
            fp minX = 0;
            fp maxX = 0;
            fp minY = 0;
            fp maxY = 0;
            fp minZ = 0;
            fp maxZ = 0;
            for (int i = 0; i < points.Count; i++)
            {
                minX = fpmath.Min(minX, points[i].x);
                maxX = fpmath.Max(maxX, points[i].x);
                minY = fpmath.Min(minY, points[i].y);
                maxY = fpmath.Max(maxY, points[i].y);
                minZ = fpmath.Min(minZ, points[i].z);
                maxZ = fpmath.Max(maxZ, points[i].z);
            }

            return new fpbounds(new fp3(minX, minY, minZ), new fp3(maxX, maxY, maxZ));
        }

        public fpbounds GetInstance(fp3 min, fp3 max)
        {
            fpbounds fpbounds = new fpbounds(min, max);
            return fpbounds;
        }

        private fpbounds(fp3 min, fp3 max)
        {
            this.min = min;
            this.max = max;
        }

        public fp3 GetClosest(fp3 point)
        {
            if (Countain(point))
            {
                return point;
            }
            else
            {
                return Clamp(point);
            }
        }

        public fp3 min { get; private set; }

        public fp3 max { get; private set; }

        public bool Countain(fp3 point)
        {
            return point.x >= min.x && point.y >= min.y && point.z >= min.z && point.x <= max.x && point.y <= max.y &&
                   point.z <= max.z;
        }

        fp3 Clamp(fp3 point)
        {
            fp x = fpmath.Clamp(point.x, min.x, max.x);
            fp y = fpmath.Clamp(point.y, min.y, max.y);
            fp z = fpmath.Clamp(point.z, min.z, max.z);
            return new fp3(x, y, z);
        }
    }
}